package com.fancy.common.utils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.time.DurationFormatUtils;

/**
 * 时间工具类
 * 
 * @author xinfeng.hu 2013-3-13下午12:15:34
 * @version 1.0.0
 * @category 杭州广桥网络技术有限公司(商业先知)
 */
public class DateUtil {

	// 完整时间
	private static final DateFormat ymdhmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static final DateFormat ymdhmFormat = new SimpleDateFormat("yyyyMMddHHmm");

	// 年月日
	private static final DateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");

	// 时分秒
	private static final DateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss");

	private static final DateFormat hmFormat = new SimpleDateFormat("HH:mm");
	private static final DateFormat ymdFormat2 = new SimpleDateFormat("yyyyMM");
	private static final DateFormat ymdFormat3 = new SimpleDateFormat("yyyyMMdd");

	public DateUtil() {
		super();
	}

	public static final String simpleFormat(Date date) {
		if (date == null)
			return "";
		return ymdhmsFormat.format(date);
	}

	public static final String dtSimpleFormat(Date date) {
		if (date == null)
			return "";
		return ymdFormat.format(date);
	}

	public static final String ymdhmFormat(Date date) {
		if (date == null)
			return "";
		return ymdhmFormat.format(date);
	}

	public static final String dtSimpleFormat2(Date date) {
		if (date == null)
			return "";
		return ymdFormat2.format(date);
	}

	public static final Date string2Date(String stringDate) throws ParseException {
		if (stringDate == null) {
			return null;
		}
		return ymdFormat.parse(stringDate);
	}
	public static final Date string3Date(String stringDate) throws ParseException {
		if (stringDate == null) {
			return null;
		}
		return ymdFormat3.parse(stringDate);
	}
	public static final String getCurDateYmd() {
		Date curDate = new Date();
		return ymdFormat3.format(curDate);
	}
	
	

	/**
	 * 返回日期时间（Add by Sunzy）
	 * 
	 * @param stringDate
	 *            字符串格式的时间
	 * @return Date 字符串所对应的时间
	 * @throws ParseException
	 */
	public static final Date string2DateTime(String stringDate) throws ParseException {
		if (stringDate == null)
			return null;
		return ymdhmsFormat.parse(stringDate);
	}

	public static final Long string2DateLong(String stringDate) throws ParseException {
		Date d = string2Date(stringDate);
		if (d == null)
			return null;
		return new Long(d.getTime());
	}

	public static final String hmsFormat(Date date) {
		if (date == null)
			return "";
		return hmsFormat.format(date);
	}

	public static final String hmFormat(Date date) {
		if (date == null)
			return "";
		return hmFormat.format(date);
	}

	/*
	 * 系统时间的转换,当前时间
	 */
	public static String getSystemDate() {
		return ymdFormat.format(new Date()).toString();
	}

	/*
	 * 系统时间的转换年
	 */
	public static String getSystemDateYear() {
		SimpleDateFormat temp = new SimpleDateFormat("yyyy");
		return temp.format(new Date()).toString();
	}

	/*
	 * 系统时间的转换有分秒的
	 */
	public static String getSystemDateall() {
		return ymdhmsFormat.format(new Date()).toString();
	}

	/*
	 * 返回两个时间相差的天数 checkPoint 是比较的类型,它的值可以从Calendar中取看
	 */
	public static int compareDate(Date date1, Date date2, int checkPoint) {
		Calendar cal1 = GregorianCalendar.getInstance();
		Calendar cal2 = GregorianCalendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);

		if (checkPoint == Calendar.MONTH) {
			// 比较月份
			int year1 = cal1.get(Calendar.YEAR);
			int month1 = cal1.get(Calendar.MONTH);
			int year2 = cal2.get(Calendar.YEAR);
			int month2 = cal2.get(Calendar.MONTH);
			return ((year1 * 12) + month1) - ((year2 * 12) + month2);
		} else if (checkPoint == Calendar.DAY_OF_YEAR) {
			// 比较天
			return (int) getResutlTime((double) (cal1.getTimeInMillis() - cal2.getTimeInMillis()) / 3600000d / 24d);
		} else if (checkPoint == Calendar.WEEK_OF_YEAR) {
			// 比较周
		}

		throw new java.lang.UnsupportedOperationException("Not yet implemented.");
	}

	// 判断天数是否比一天多小时分,秒,如果是的话,就算一天
	public static int getResutlTime(double day) {
		int temp = String.valueOf(day).indexOf(".");
		if (day - Integer.parseInt(String.valueOf(day).substring(0, temp)) > 0) {
			return (int) (day + 1);
		} else {
			return (int) day;
		}
	}

	/*
	 * 在一特定的时间内加上n个月后得到的时间
	 */
	public static Date addDateMonth(Date gmtUpload, BigDecimal period) {
		Calendar cal = GregorianCalendar.getInstance();
		cal.setTime(gmtUpload);
		int month = cal.get(Calendar.MONTH) + 1;
		int addyear = 0;
		int addmonth = 0;
		if (period.intValue() > 12) {
			addyear = period.intValue() / 12;
			addmonth = period.intValue() % 12;
			if ((month + addmonth) > 12) {
				addyear = addyear + 1;
				addmonth = addmonth + month - 12;
			} else {
				addmonth = addmonth + month;
			}
		}
		cal.add(Calendar.YEAR, addyear);
		cal.add(Calendar.MONTH, addmonth);
		return cal.getTime();
	}

	/**
	 * 在一个特定的时间后加上n天
	 * 
	 * @param time
	 *            特定时间
	 * @param period
	 *            向后或者向前的参数 －2 ， 2
	 * @return Date
	 */
	public static Date addDateDay(Date time, int period) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(time);
		cal.add(GregorianCalendar.DATE, period);
		return cal.getTime();

	}
	
	/**
	 * 在一个特定的时间后加上月
	 * 
	 * @param time
	 *            特定时间
	 * @param month
	 *            向后或者向前的参数 －2 ， 2
	 * @return Date
	 */
	public static Date addDateMonth(Date time, int month) {
		Calendar cal = new GregorianCalendar();
		cal.setTime(time);
		cal.add(GregorianCalendar.MONTH, month);
		return cal.getTime();

	}

	// 判断闰年
	public static boolean CheckLeap(int year) {
		if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
			return true;
		else
			return false;
	}

	/*
	 * 删除时间的"-"
	 */
	public static String Delete(String something) {
		String dealwithsomething = "";
		int tempflag = 0;
		int temp = 0;
		for (int i = 0; i < 2; i++) {
			tempflag = something.indexOf("-", temp);
			dealwithsomething = dealwithsomething + something.substring(temp, tempflag).trim();
			temp = tempflag + 1;
			if (i == 1) {
				dealwithsomething = dealwithsomething + something.substring(temp).trim();
			}
		}
		return dealwithsomething;
	}

	/**
	 * Format Date into which format you define
	 * 
	 * @param date
	 *            (java.util.Date)
	 * @param newFormat
	 *            (String)
	 * @return String example formatDate(date, "MMMM dd, yyyy") = July 20, 2000
	 */
	public static String formatDate(Date date, String newFormat) {
		if ((date == null) || (newFormat == null)) {
			return null;
		}

		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(newFormat);

		return formatter.format(date);
	}

	/**
	 * 格式话时间
	 * 
	 * @param date
	 * @return
	 */
	public static String getTimeYMD(Date date) {
		if (date == null) {
			return "";
		}
		return ymdFormat.format(date);
	}

	/**
	 * 获得与当前时间相差毫秒数
	 * 
	 * @param date
	 * @return
	 */
	public static Long getTimesBetweenNow(Date date) {
		if (date == null) {
			return 0L;
		}
		Date now = new Date();
		return getTimesBetween(now, date);
	}

	/**
	 * 获得两个时间之间相差的毫秒数
	 * 
	 * @param now
	 * @param date
	 * @return
	 */
	public static Long getTimesBetween(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return 0L;
		}
		return date1.getTime() - date2.getTime();
	}

	/**
	 * 计算时间
	 * 
	 * @param time
	 * @return
	 */
	public static String getTimePrompt(long time) {
		if (time <= 1000) {
			return "刚刚";
		}
		String s = DurationFormatUtils.formatDurationHMS(time);
		String[] times = s.replaceAll("[\\.]\\d{3}", "").split(":");
		if (!(times[0] != null && times[0].matches("\\d+"))) {
			return null;
		}
		if (Integer.parseInt(times[0]) >= 24) {
			int days = getDaysBetween(time);
			if (days > 30) {
				return null;
			}
			return days + "天前";
		}
		if (Integer.parseInt(times[0]) > 0 && Integer.parseInt(times[0]) < 24) {
			return Integer.parseInt(times[0]) + "小时前";
		}
		if (times[1] != null && times[1].matches("\\d+") && Integer.parseInt(times[1]) > 0 && Integer.parseInt(times[1]) <= 60) {
			return Integer.parseInt(times[1]) + "分钟前";
		}
		if (times[2] != null && times[2].matches("\\d+") && Integer.parseInt(times[2]) > 0 && Integer.parseInt(times[2]) <= 60) {
			return Integer.parseInt(times[2]) + "秒前";
		}
		return "刚刚";
	}

	/**
	 * 计算天数
	 * 
	 * @return 天数
	 */
	public static int getDaysBetween(long between_days) {
		if (between_days <= 0) {
			return 0;
		}
		Long days = between_days / (1000 * 3600 * 24);
		return Integer.parseInt(String.valueOf(days));
	}

	/**
	 * 获得日期之间的差
	 * 
	 * @param date
	 * @return
	 */
	public static String getInfoBetweenNow(Date date) {
		if (date == null) {
			return "刚刚";
		}
		long time = getTimesBetweenNow(date);
		String str = getTimePrompt(time);
		return str == null ? getTimeYMD(date) : str;
	}

	public static void main(String args[]) {
		Date nowDate = new Date();
		Date newDate = nowDate;
		System.out.println("nowDate==" + DateUtil.getCurDateYmd());
		System.out.println("newDate==" + newDate);

		System.out.println("");
		System.out.println("nowDate==" + nowDate);
		System.out.println("newDate==" + newDate);
	}
}
